package com.foxzzz.temporary;

public class Solution5 {
    private boolean LeftEqulasRight(String s,double index,int step){
        if(step==0)
            return true;
        int left=0;
        int right=0;
        if((int)index!=index) {
            left = (int) (index + 0.5 - step);
            right=(int)(index-0.5+step);
        }else {
            left = (int) (index - step);
            right = (int) (index + step);
        }
        if(left<0||right>=s.length())
            return false;
        return s.charAt(left)==s.charAt(right);
    }

    public String longestPalindrome(String s) {
       if(s.equals("")) return "";
        double index=0d;
        int maxStep=0;
        for (double i= 0; i < s.length(); i=i+0.5) {
                int step=0;
                while (LeftEqulasRight(s,i,step)){
                    if(step>maxStep){
                        maxStep=step;
                        index=i;
                    }
                    //优先选择下标为整数的 因为maxStep一样的基础下 此时长度比下标不为整的大1
                    if(step==maxStep && ((int) i) == i){
                        index=i;
                    }
                    step++;
                }
        }
        int left=0;
        int right=0;
        if(((int) index)!=index){
            left=(int)  (index + 0.5 - maxStep);
            right=(int) (index - 0.5 + maxStep);
        }else {
            left=(int)(index-maxStep);
            right=(int)(index+maxStep);
        }

       return s.substring(left,right+1);
    }

    public static void main(String[] args) {
        System.out.println(new Solution5().longestPalindrome("aaa"));
    }
}
